package com.example.myapplication.New;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LibraryDetailsBean;
import com.example.myapplication.ShuJu.NewsCarouselmapBean;
import com.example.myapplication.ShuJu.NewsDetailsBean;
import com.example.myapplication.ShuJu.ViewlistBean;
import com.example.myapplication.apter.NewsCarouselmapApter;
import com.example.myapplication.apter.RecommendednewApter;
import com.example.myapplication.apter.ViewlistbeanApter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URL;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class NewsDetailsActivity extends AppCompatActivity {

    private ImageView newsdetalis_bloak;
    private ImageView newsdetalist_img;
    private TextView newsDetailContent;
    private RecyclerView renewsdetalisthot;
    private RecyclerView renewsdetalist;
    private String newsDetailsActivityID;
    private OkHttpClient okHttpClient;
    private Request request;
    private Gson gson;
    private Handler handler;
    private NewsDetailsBean newsDetailsBean;
    private NewsCarouselmapBean carouselmapBean;
    private EditText news_content;
    private ViewlistBean viewlistBean;
    private TextView newsbut_view;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private TextView title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_details);
        initview();
        Intent intent = getIntent();
        newsDetailsActivityID = intent.getStringExtra("NewsDetailsActivityID");
        //详情内容
        sendbean();
        //推荐新闻
        //recommendednew();
        //返回
        newsbloak();
        //评论列表
        viewlist();

    }

    private void snedbutlist() {
        newsbut_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userinput = news_content.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("newsId",String.valueOf(newsDetailsBean.getData().getId()));
                    jsonObject.put("content",userinput);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = RequestBody.create(jsonObject.toString(),JSON);
                request = new Request.Builder()
                        .url("http://124.93.196.45:10001/prod-api/press/pressComment")
                        .addHeader("Authorization", TokenBean.token)
                        .post(requestBody)
                        .build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        Log.e("onFailure","json失败");
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //评论列表
                                viewlist();
                                Toast.makeText(NewsDetailsActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }

        });
    }

    private void viewlist() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/press/comments/list?newsId="+newsDetailsActivityID+"&pageNum=1&pageSize=10")
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =response.body().string();
                Log.d("sendBean",json+"xxx");
                viewlistBean = gson.fromJson(json,  ViewlistBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        binviewlistbean();
                    }
                });
            }
        });
    }

    private void binviewlistbean() {
        ViewlistbeanApter viewlistbeanApter = new ViewlistbeanApter(NewsDetailsActivity.this,viewlistBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(NewsDetailsActivity.this);
        renewsdetalisthot.setAdapter(viewlistbeanApter);
        renewsdetalisthot.setLayoutManager(linearLayoutManager);
    }

    private void newsbloak() {
        newsdetalis_bloak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewsDetailsActivity.this.finish();
            }
        });
    }

    private void recommendednew() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/press/press/list?hot=Y")
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =response.body().string();
                carouselmapBean = gson.fromJson(json,  NewsCarouselmapBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        gebean();
                    }
                });
            }
        });
    }

    private void gebean() {
        RecommendednewApter recommendednewApter = new RecommendednewApter(NewsDetailsActivity.this,carouselmapBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(NewsDetailsActivity.this);
        renewsdetalisthot.setAdapter(recommendednewApter);
        renewsdetalisthot.setLayoutManager(linearLayoutManager);
    }

    private void sendbean() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/press/press/"+newsDetailsActivityID)
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =response.body().string();
                newsDetailsBean = gson.fromJson(json,  NewsDetailsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(NewsDetailsActivity.this).load(TokenBean.IP+newsDetailsBean.getData().getCover()).into(newsdetalist_img);
                        newsDetailContent.setText(Html.fromHtml(newsDetailsBean.getData().getContent()));
                        title.setText(newsDetailsBean.getData().getTitle());
                        //发布评论
                         snedbutlist();
                    }
                });
            }
        });
    }

    private void initview() {
        newsdetalis_bloak = findViewById(R.id.newsdetalis_bloak);
        newsdetalist_img = findViewById(R.id.newsdetalist_img);
        renewsdetalisthot = findViewById(R.id.renewsdetalisthot);
        renewsdetalist = findViewById(R.id.renewsdetalist);
        news_content = findViewById(R.id.news_content);
        newsDetailContent = findViewById(R.id.newsDetailContent);
        newsbut_view = findViewById(R.id.newsbut_view);
        title = findViewById(R.id.title);

        okHttpClient = new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
    }
}